<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="类型选择">
              <el-select v-model="form.type" placeholder="请选择" clearable>
                <el-option
                  v-for="item in typeList"
                  :key="item.type"
                  :label="item.name"
                  :value="item.type"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="省份">
              <el-select v-model="form.province" placeholder="请选择" clearable>
              
                <el-option
                  v-for="item in options"
                  :key="item.path"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标题">
              <el-input v-model="form.title" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
               <!-- <Storage></Storage> -->
            </el-form-item>
            <el-form-item>
              <el-button
                v-if="hasPermission('AdminMaterialImageProvider.saveMaterialImage')"
                type="primary"
                @click="toRoutes('materialPhotoAdd')"
              >添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="title" label="标题" />
        <el-table-column label="封面图">
          <template slot-scope="scope">
            <div>
              <img style="height:40px;width:40px" :src="scope.row.miniUrl | imgUrlFn" alt />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="类型">
          <template slot-scope="scope">{{ scope.row.type }}</template>
        </el-table-column>
        <el-table-column prop="province" label="发放省份">
          <template slot-scope="scope">{{scope.row.province | getprovince(options)}}</template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" />
        <el-table-column prop="createdAt" label="添加时间" />
        <el-table-column prop="isRecom" label="是否推荐" />
        <el-table-column prop="status" label="状态" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="showPic(scope.row.miniUrl)">查看海报</el-button>
            <el-button
              v-if="hasPermission('AdminMaterialImageProvider.updateMaterialImage')"
              type="text"
              size="small"
              @click="toRoutes('materialPhotoEdit',scope.row.id)"
            >编辑</el-button>
            <el-button
              v-if="hasPermission('AdminMaterialImageProvider.deleteMaterialImageById')"
              type="text"
              size="small"
              @click="deleteList(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="listForm.pageNum"
            :page-sizes="page_sizes"
            :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  materialPhotoList,
  materialPhotoListDelete
} from "@/api/materialPhoto";
import { materialtypeList } from "@/api/materialPlace";
import { policy } from "@/api/policy";
import { regionDropDown } from "@/api/area";
export default {
  name: "MaterialPhotoIndex",
  filters: {
    cardType: function(e) {
      return window.helper.cardType(String(e));
    },
    imgUrlFn: function(e) {
      return localStorage.getItem("imgUrlPrefix") + e;
    },
    getprovince(val, arr) {
      // console.log(val, arr);

      let label = arr.filter(res => {
        if (res.value == val) {
          return res.label;
        }
      });
      return label[0].label;
    }
  },
  data() {
    return {
      val: "1",
      dates: "",
      tableLoading: true,
      form: {
        pageNum: 1,
        pageSize: 10,
        title: "",
        type: null
      },
      tableData: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10
      },
      options: [],
      typeList: []
    };
  },
  mounted() {
    this.getArea();
    this.getTypeList();
    
  },
  created() {},
  methods: {
    // 按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    // 成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    // 查询
    searchFn() {
      this.form.pageNum = 1;
      this.getList(this.form);
    },
    //获取类型下拉列表
    getTypeList() {
      let datas = {};
      materialtypeList(datas).then(res => {
        this.typeList = res.data.data;
      });
    },
    // 省市区
    getArea() {
      regionDropDown().then(res => {
        const newArr = [{ value: 0, label: "全国" }];
        res.data.data.forEach((item1, index) => {
          newArr.push({
            value: item1.id,
            label: item1.name
          });
        });
        this.options = newArr;
        this.getList(this.listForm);
      });
    },
    // 列表数据获取
    getList(e) {
      this.tableLoading = true;
      const datas = e;
      if (datas.type == "") {
        datas.type = null;
      }
      materialPhotoList(datas)
        .then(res => {
          if (!res.data.code) {
            res.data.data.list.map(item => {
              item.isRecom = item.isRecom == 0 ? "不推荐" : "推荐";
              item.status = item.status == 0 ? "正常" : "下架";
            });
            this.tableData = res.data.data.list;

            this.page_totals = res.data.data.count;
            // if (this.tableData.length > 0) {
            //   this.tableData.forEach(item => {
            //     item.isRecom = item.isRecom == 0 ? "不推荐" : "推荐";
            //     item.status = item.status == 0 ? "正常" : "下架";
            //     this.tableData.forEach(item => {
            //       this.typeList.forEach(list => {
            //         if (item.type == list.type) {
            //           item.type = list.name;
            //         }
            //       });
            //     });
            //     this.options.forEach(list => {
            //       if (item.province == list.value) {
            //         item.province = list.label;
            //       }
            //     });
            //   });
            // }
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 路由跳转
    toRoutes(e, id) {
      const route = { name: e, query: { id: id } };
      this.$router.push(route);
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getList(this.form);
    },
    // 删除
    deleteList(e) {
      const datas = { id: e };
      console.log(datas);
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          console.log(datas);
          materialPhotoListDelete(datas).then(res => {
            if (!res.data.code) {
              this.succStatus("删除");
              this.getList(this.form);
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    showPic(img) {
      const imgUrl = localStorage.getItem("imgUrlPrefix") + img;
      window.open(imgUrl);
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
